<?php

class Setup_PasswordController extends Ext_Controller_Action_Front
{
    public function init()
    {
        parent::init();
    }

    
    public function preDispatch()
    {
        parent::preDispatch();
        
        $userId = Ext_Auth_Front::getInstance()->getIdentity();
        if(!$userId){
            return "";
        }
        
        $roleId = Ext_User::getInstance()->show($userId)->buildRole()->role->roleId;
        $acl = Ext_Acl::getInstance()->build($roleId);
        if(!$acl->isAllowed($roleId, 'setup', 'account')){
            $this->_forward("show-access-denied", "auth", "account");
        }
    }
    
    public function savePostAction()
    {
        $viewResult = new Ext_View_Result();

        $oldPassword        = $this->filter->filter($this->_request->getPost('oldPassword'));
        $newPassword        = $this->filter->filter($this->_request->getPost('newPassword'));
        $confirmNewPassword = $this->filter->filter($this->_request->getPost('confirmNewPassword'));
  
        $validater = new Ext_Validate();
        $validater->addValidater(new Add_Setup_Password_SavePost_Validate_OldPassword($oldPassword));
        $validater->addValidater(new Add_Setup_Password_SavePost_Validate_NewPassword($newPassword, $confirmNewPassword));
        if(!$validater->isValid()){
            $viewResult->addMessages($validater->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        
        $userId = Ext_Auth_Front::getInstance()->getIdentity();
        
        $data = new Ext_User_Verify_Data();
        $data->setUserId($userId);
        $data->setPassword($oldPassword);
        $verify = new Ext_User_Verify($data);
        $verifyResult = $verify->verify();
        if(!$verifyResult->isValid()){
            $viewResult->addMessages($verifyResult->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        
        $editPassword = new Ext_User_Edit_Password($userId);
        $editResult = $editPassword->edit($newPassword);
        if(!$editResult->isValid()){
            $viewResult->addMessages($editResult->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        
        $viewResult->addMessages($this->translater->_("Submit success."));
        $viewResult->setResult(1);
        $viewResult->setNoRender();
    }

    public function showFormAction()
    {
        $userId = Ext_Auth_Front::getInstance()->getIdentity();

        $viewForm = new Ext_View_Form_Setup_Password();

        $this->view->form    = $viewForm;
    }
    
}

?>